import { createRouter, createWebHashHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import ProfileEdit from '@/views/ProfileEdit.vue'

const router = createRouter({
  history: createWebHashHistory(), //
  routes: [
    // {
    //   path: '/',
    //   name: 'home',
    //   component: HomeView
    // },
    // {
    //   path: '/about',
    //   name: 'about',
    //   // route level code-splitting
    //   // this generates a separate chunk (About.[hash].js) for this route
    //   // which is lazy-loaded when the route is visited.
    //   component: () => import('../views/AboutView.vue') // 动态导入 实现路由懒加载
    // }
    {
      path: '/',
      redirect: '/profile'
    },
    {
      path: '/profile',
      component: ProfileEdit,
      meta: { title: '个人资料' }
    },
    {
      path: '/address',
      component: () => import('../views/AddressManager.vue'),
      meta: { title: '地址列表' }
    },
    {
      path: '/address/edit/:id?',
      component: () => import('../views/AddressEdit.vue'),
      meta: { title: '地址管理' }
    }
  ]
})

// router 控制了前端页面的展示 页面中的标题
router.afterEach((obj) => {
  // 只要路由匹配了页面 页面展示 就会执行
  document.title = obj.meta.title as string
})

export default router
